/**********************************************************
 * NewAnnouncementAction.java
 * Created by ankur
 * Last modified Date: Aug 4, 2008 9:34:30 AM by ankur
 * Version : 0.1
 * Copyright (c) 2008 - 2008 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import org.apache.log4j.Logger;
import java.util.Date;
import org.joda.time.DateTime;
import com.opensymphony.xwork2.ActionSupport;
import com.printeverywhere.delegator.AnnouncementServiceDelegator;
import com.printeverywhere.dto.Announcement;
import com.printeverywhere.util.DataValidator;
import static com.printeverywhere.util.MessageConstants.*;

/**
 * The Class NewAnnouncementAction.
 */
public class NewAnnouncementAction extends AdminEngineManagement {
	
	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(NewAnnouncementAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	
	/**
	 * Execute.
	 * 
	 * @return the string
	 * 
	 * @throws Exception the exception
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		if(announcement!=null && announcement.getId()>0){
			announcement = AnnouncementServiceDelegator.getAnnouncement(announcement.getId());
			date = announcement.getDate();
			time = announcement.getDate();
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Save.
	 * 
	 * @return the string
	 * 
	 * @throws Exception the exception
	 */
	public String save() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("save() - start"); //$NON-NLS-1$
		}

		if(DataValidator.isNull(announcement.getSubject()))addActionError(SUBJECT_CANNOT_BE_NULL);
		if(DataValidator.isNull(announcement.getMessage()))addActionError(MESSAGE_CANNOT_BE_NULL);
		if(!getActionErrors().isEmpty()){
			if (logger.isDebugEnabled()) {
				logger.debug("save() - end"); //$NON-NLS-1$
			}
			return ERROR;
		}
		DateTime date = new DateTime(this.date);
		DateTime time = new DateTime(this.time);
		date = date.plusHours(time.getHourOfDay()).plusMinutes(time.getMinuteOfHour());
		announcement.setDate(date.toDate());
		AnnouncementServiceDelegator.insertAnnouncement(announcement);

		if (logger.isDebugEnabled()) {
			logger.debug("save() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}
	
	/**
	 * Delete.
	 * 
	 * @return the string
	 * 
	 * @throws Exception the exception
	 */
	public String delete() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("delete() - start"); //$NON-NLS-1$
		}

		AnnouncementServiceDelegator.deleteAnnouncement(announcement);

		if (logger.isDebugEnabled()) {
			logger.debug("delete() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}
	
	/**
	 * Copy.
	 * 
	 * @return the string
	 * 
	 * @throws Exception the exception
	 */
	public String copy() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("copy() - start"); //$NON-NLS-1$
		}

		if(announcement!=null && announcement.getId()>0){
			announcement = AnnouncementServiceDelegator.getAnnouncement(announcement.getId());
			date = announcement.getDate();
			time = announcement.getDate();
			announcement.setId(null);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("copy() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/** The date. */
	private Date date;
	
	/** The time. */
	private Date time;
	
	/** The announcement. */
	private Announcement announcement;


	/**
	 * Gets the announcement.
	 * 
	 * @return the announcement
	 */
	public Announcement getAnnouncement() {
		return announcement;
	}

	/**
	 * Sets the announcement.
	 * 
	 * @param announcement the new announcement
	 */
	public void setAnnouncement(Announcement announcement) {
		this.announcement = announcement;
	}

	/**
	 * Gets the date.
	 * 
	 * @return the date
	 */
	public Date getDate() {
		return date;
	}

	/**
	 * Sets the date.
	 * 
	 * @param date the new date
	 */
	public void setDate(Date date) {
		this.date = date;
	}

	/**
	 * Gets the time.
	 * 
	 * @return the time
	 */
	public Date getTime() {
		return time;
	}

	/**
	 * Sets the time.
	 * 
	 * @param time the new time
	 */
	public void setTime(Date time) {
		this.time = time;
	}
}